package com.yc.sole.erp.controller;

import com.yc.sole.common.domain.BaseIndexResp;
import com.yc.sole.erp.biz.SupplierBiz;
import com.yc.sole.erp.request.SupplierQueryReq;
import com.yc.sole.erp.request.SupplierReq;
import com.yc.sole.erp.response.SupplierResp;
import com.yc.sole.framework.mybatis.page.BaseBatchIdsReq;
import com.yc.sole.framework.mybatis.page.BasePageReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.DeleteGroup;
import com.yc.sole.framework.validate.SaveGroup;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 供应商
 *
 * @author yizuomin
 * @date 2023-05-07
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class SupplierController {

    private final SupplierBiz supplierBiz;

    /**
     * 新增供应商表
     *
     * @param supplierReq
     * @return RestResult
     */
    @PreAuth(value = "base-data:supplier:save", description = "新增供应商")
    @PostMapping("/api/base-data/supplier/save")
    public Result<Void> saveSupplier(@RequestBody @Validated(SaveGroup.class) SupplierReq supplierReq) {
        supplierBiz.saveSupplier(supplierReq);
        return Result.success();
    }

    /**
     * 通过Id修改供应商表
     *
     * @param supplierReq
     * @return RestResult
     */
    @PreAuth(value = "base-data:supplier:update", description = "修改供应商")
    @PostMapping("/api/base-data/supplier/update")
    public Result<Void> updateSupplierById(@RequestBody @Validated(UpdateGroup.class) SupplierReq supplierReq) {
        supplierBiz.updateSupplierById(supplierReq);
        return Result.success();
    }

    /**
     * 通过id删除供应商表
     *
     * @param baseDeleteReq
     * @return RestResult
     */
    @PreAuth(value = "base-data:supplier:delete", description = "删除供应商")
    @PostMapping("/api/base-data/supplier/delete")
    public Result<Void> deleteSupplier(@RequestBody @Validated(DeleteGroup.class) BaseBatchIdsReq baseDeleteReq) {
        supplierBiz.deleteSupplier(baseDeleteReq.getIds());
        return Result.success();
    }


    /**
     * 分页查询供应商表
     *
     * @param basePageReq
     * @return Result
     */
    @PreAuth(value = "base-data:supplier:select", description = "查询供应商")
    @PostMapping("/api/base-data/supplier/page")
    public Result<Paging<SupplierResp>> selectSupplierPage(@RequestBody @Validated BasePageReq<SupplierQueryReq> basePageReq) {
        return Result.data(supplierBiz.selectSupplierPage(basePageReq.toPage(), basePageReq.getQuery()));
    }

    /**
     * 根据名称查询所有供应商
     *
     * @param supplierPageReq
     * @return Result
     */
    @PreAuth(value = "base-data:supplier:select", description = "根据名称查询所有供应商")
    @PostMapping("/api/base-data/supplier/list")
    public Result<List<SupplierResp>> selectSupplierByName(@RequestBody @Validated SupplierQueryReq supplierPageReq) {
        return Result.data(supplierBiz.selectSupplierByName(supplierPageReq.getSupplierName()));
    }

    /**
     * 查询带索引的供应商信息
     *
     * @return Result
     */
    @PreAuth(value = "base-data:supplier:select", description = "查询带索引的供应商信息")
    @PostMapping("/api/base-data/supplier/index")
    public Result<List<BaseIndexResp<SupplierResp>>> selectAllSupplierWithIndex(@RequestBody SupplierQueryReq queryReq) {
        return Result.data(supplierBiz.selectAllSupplierWithIndex(queryReq));
    }

    /**
     * 根据id查询所有供应商
     *
     * @param supplierId
     * @return Result
     */
    @PreAuth(value = "base-data:supplier:select", description = "根据名称查询所有供应商")
    @PostMapping("/api/base-data/supplier/{supplierId}")
    public Result<SupplierResp> selectSupplierById(@PathVariable Long supplierId) {
        return Result.data(supplierBiz.selectSupplierById(supplierId));
    }
}
